package com.xywh.wanandroid

import androidx.multidex.MultiDex
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadSir
import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.connection.FileDownloadUrlConnection
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.mmkv.MMKV
import com.xywh.composeLibrary.ext.stateComposeConfig
import com.xywh.wanandroid.activity.ErrorActivity
import com.xywh.wanandroid.compose_ui.MainActivity
import com.xywh.wanandroid.event.AppViewModel
import com.xywh.wanandroid.event.EventViewModel
import com.xywh.wanandroid.util.ScreenUtil
import com.xywh.wanandroid.weight.loadCallBack.EmptyCallback
import com.xywh.wanandroid.weight.loadCallBack.ErrorCallback
import com.xywh.wanandroid.weight.loadCallBack.LoadingCallback
import dagger.hilt.android.HiltAndroidApp
import me.hgj.jetpackmvvm.base.BaseApp
import me.hgj.jetpackmvvm.ext.util.jetpackMvvmLog


//Application全局的ViewModel，里面存放了一些账户信息，基本配置信息等
val appViewModel: AppViewModel by lazy { MyApp.appViewModelInstance }

//Application全局的ViewModel，用于发送全局通知操作
val eventViewModel: EventViewModel by lazy { MyApp.eventViewModelInstance }

@HiltAndroidApp
class MyApp : BaseApp() {

    //判断是否在前后台
    private var count = 0

    //第一次不对程序进行处理里
    private var isFirst = true
    private val isBack = false
    private var time: Long = -2

    companion object {
        lateinit var instance: MyApp
        lateinit var eventViewModelInstance: EventViewModel
        lateinit var appViewModelInstance: AppViewModel
    }

    override fun onCreate() {
        super.onCreate()
        MultiDex.install(this)
        MMKV.initialize(this)
        instance = this

        ScreenUtil.init(this)
        eventViewModelInstance = getAppViewModelProvider().get(EventViewModel::class.java)
        appViewModelInstance = getAppViewModelProvider().get(AppViewModel::class.java)

        jetpackMvvmLog = BuildConfig.DEBUG
        //界面加载管理 初始化
        LoadSir.beginBuilder()
            .addCallback(LoadingCallback())//加载
            .addCallback(ErrorCallback())//错误
            .addCallback(EmptyCallback())//空
            .setDefaultCallback(SuccessCallback::class.java)//设置默认加载状态页
            .commit()
        FileDownloader.setupOnApplicationOnCreate(this)
            .connectionCreator(
                FileDownloadUrlConnection.Creator(
                    FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15000) // set connection timeout.
                        .readTimeout(15000) // set read timeout.
                )
            )
            .commit()

        //防止项目崩溃，崩溃后打开错误界面
        CaocConfig.Builder.create()
            .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
            .enabled(true)//是否启用CustomActivityOnCrash崩溃拦截机制 必须启用！不然集成这个库干啥？？？
            .showErrorDetails(false) //是否必须显示包含错误详细信息的按钮 default: true
            .showRestartButton(false) //是否必须显示“重新启动应用程序”按钮或“关闭应用程序”按钮default: true
            .logErrorOnRestart(false) //是否必须重新堆栈堆栈跟踪 default: true
            .trackActivities(true) //是否必须跟踪用户访问的活动及其生命周期调用 default: false
            .minTimeBetweenCrashesMs(2000) //应用程序崩溃之间必须经过的时间 default: 3000
            .restartActivity(MainActivity::class.java) // 重启的activity
            .errorActivity(ErrorActivity::class.java) //发生错误跳转的activity
            .apply()

        //初始化Bugly
        CrashReport.initCrashReport(applicationContext, "2e36da27b2", false)

        //        CrashReport.testJavaCrash();

        stateComposeConfig(instance)
    }


}